/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginFrame.java
 *
 * Created on 23.Kas.2009, 20:29:33
 */
package edu.iyte.ceng431.hw2.pos.view;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jvnet.substance.skin.SubstanceBusinessLookAndFeel;
import edu.iyte.ceng431.hw2.pos.controller.RegisterController;
import edu.iyte.ceng431.hw2.pos.db.ConnectDB;
import edu.iyte.ceng431.hw2.pos.db.Search;
import edu.iyte.ceng431.hw2.pos.model.Cashier;
import edu.iyte.ceng431.hw2.pos.model.Register;

/**
 * 
 * @author $@D!$t
 */
public class LoginFrame extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Integer> registers;
	private List<Cashier> cashiers;

	/** Creates new form LoginFrame */
	public LoginFrame() {
		registers = Search.getAllRegisters();
		cashiers = Search.getAllCashiers();
		initComponents();
		this.setLocationRelativeTo(null);
		Iterator<Integer> it = registers.iterator();
		while (it.hasNext()) {
			this.registerComboBox.addItem(it.next());
		}

		Iterator<Cashier> itt = cashiers.iterator();
		while (itt.hasNext()) {
			this.cashierComboBox.addItem(itt.next().getName());
		}

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {

		this.setTitle("Login");
		jPanel1 = new javax.swing.JPanel();
		registerComboBox = new javax.swing.JComboBox();
		cashierComboBox = new javax.swing.JComboBox();
		registerLabel = new javax.swing.JLabel();
		cashierLabel = new javax.swing.JLabel();
		initialBalanceLabel = new javax.swing.JLabel();
		initialBalanceTextField = new javax.swing.JTextField();
		loginButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);

		registerLabel.setText("Select the Register to Work");

		cashierLabel.setText("select the Cashier");

		initialBalanceLabel.setText("Initial Register Balance");

		initialBalanceTextField.setText("0");

		loginButton.setText("LOGIN");
		loginButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				loginButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								jPanel1Layout
																										.createSequentialGroup()
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																false)
																														.addComponent(
																																registerComboBox,
																																0,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.addComponent(
																																registerLabel,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE))
																										.addGap(
																												40,
																												40,
																												40)
																										.addGroup(
																												jPanel1Layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																cashierComboBox,
																																0,
																																136,
																																Short.MAX_VALUE)
																														.addComponent(
																																cashierLabel)))
																						.addGroup(
																								jPanel1Layout
																										.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.TRAILING,
																												false)
																										.addComponent(
																												initialBalanceTextField,
																												javax.swing.GroupLayout.Alignment.LEADING)
																										.addComponent(
																												initialBalanceLabel,
																												javax.swing.GroupLayout.Alignment.LEADING,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE))))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGap(
																				130,
																				130,
																				130)
																		.addComponent(
																				loginButton)))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(22, 22, 22)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																registerLabel)
														.addComponent(
																cashierLabel))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																registerComboBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cashierComboBox,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addComponent(initialBalanceLabel)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												initialBalanceTextField,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												18, Short.MAX_VALUE)
										.addComponent(loginButton)
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addComponent(
						jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		pack();
	}

	private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_loginButtonActionPerformed

		Cashier cas = new Cashier(cashiers.get(
				cashierComboBox.getSelectedIndex()).getId(), cashierComboBox
				.getSelectedItem().toString());
		try {
			double initialBalance = Double.parseDouble(initialBalanceTextField
					.getText());
			if (initialBalance < 0)
				throw new NumberFormatException();
			Register reg = new Register(Integer.parseInt(registerComboBox
					.getSelectedItem().toString()), initialBalance, cas);
			reg.makeSale();
			MainFrame mainView = new MainFrame(reg, cas);
			new RegisterController(mainView, reg);
			mainView.setVisible(true);
			this.setVisible(false);
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Invalid Initial Balance.",
					"Error", JOptionPane.ERROR_MESSAGE);
		}

	}// GEN-LAST:event_loginButtonActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					UIManager
							.setLookAndFeel(new SubstanceBusinessLookAndFeel());
				} catch (UnsupportedLookAndFeelException ex) {
					Logger.getLogger(LoginFrame.class.getName()).log(
							Level.SEVERE, null, ex);
				}

				new ConnectDB();
				new LoginFrame().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JComboBox cashierComboBox;
	private javax.swing.JLabel cashierLabel;
	private javax.swing.JLabel initialBalanceLabel;
	private javax.swing.JTextField initialBalanceTextField;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JButton loginButton;
	private javax.swing.JComboBox registerComboBox;
	private javax.swing.JLabel registerLabel;

	// End of variables declaration//GEN-END:variables

	/**
	 * @return the cashierComboBox
	 */
	public javax.swing.JComboBox getCashierComboBox() {
		return cashierComboBox;
	}

	/**
	 * @param cashierComboBox
	 *            the cashierComboBox to set
	 */
	public void setCashierComboBox(javax.swing.JComboBox cashierComboBox) {
		this.cashierComboBox = cashierComboBox;
	}

	/**
	 * @return the registerComboBox
	 */
	public javax.swing.JComboBox getRegisterComboBox() {
		return registerComboBox;
	}

	/**
	 * @param registerComboBox
	 *            the registerComboBox to set
	 */
	public void setRegisterComboBox(javax.swing.JComboBox registerComboBox) {
		this.registerComboBox = registerComboBox;
	}
}
